<template>
	<view class="content">
		<view class="topcomtent">
		<view class="login-screen-title">
									<!--<i class="f7-icons">add_round</i>-->
		
		<view class="gdzc">Hi,欢迎登录</view>
		<view class="gdzc_lable">国泰君安证券 － 固定资产管理系统</view>
		</view>
		</view>
		<view style="padding: 0 25rpx;">
			<view
			style="height: 130rpx;line-height: 130rpx;font-weight: bold;
			color:#6D8AF9;font-size: 38rpx;padding-left: 15rpx;"
			>登 录</view>
			<view class="row" style=" border-bottom: 1px solid #f8f8f8 !important;">
						<view style="position:absolute;margin-top: 8px;">
							<image src="../../static/images/icon-myuser-ed-login.png" class="item-icon" mode=""></image>
						</view>
						<input v-model="username" type="text" placeholder="请输入用户名" />
					</view>
			
					<view class="row" style=" border-bottom: 1px solid #f8f8f8 !important;">
						<view style="position:absolute;margin-top: 8px;">
							<image src="../../static/images/icon-password.png"  class="item-icon" mode="" ></image>
						</view>
						<input v-model="pwd" type="password" placeholder="请输入密码" />
					</view>
					<view class="row">
						<view style="margin-right: 15rpx;">
							<image :src="checkurl"  class="item-icon" mode="" @tap="checkicon"></image>
							
						</view>
						<view style="font-size: 28rpx;margin-top: 2px;color:#6D8AF9;">
							保存密码
						</view>
			
						</view>
					<view class="btg">
						<button type="primary" @tap="btn" :disabled="loginStr!='登录'">
							<span>{{loginStr}}</span>
							   
							</button>
					</view>
		</view>
	</view>
</template>

<script>
	import md5 from './md5.js'
	var self;
	export default {
		data() {
			return {
				username: '',
				pwd: '',
				ischeck:0,
				checkurl:"../../static/images/icon-uncheck.png",
				isLoading:false,
				loginStr:"登录"
			}
		},
		onLoad(){
			
			
			
			uni.removeStorageSync("IdexSumCount");
			uni.removeStorageSync("IdexSumCount_ZY");
			uni.removeStorageSync("IdexSumCount_XZ");
			uni.removeStorageSync("IdexSumCount_JY");
			uni.removeStorageSync("IdexSumCount_YF");
			uni.removeStorageSync("MyAPPUserModel");
			
		},
		onBackPress(options) {
			//plus.runtime.quit();
			return true //return true的意思是禁止返回到上一个界面
			},
		created() {
			
			uni.removeStorageSync("IdexSumCount");
			uni.removeStorageSync("IdexSumCount_ZY");
			uni.removeStorageSync("IdexSumCount_KC");			
			uni.removeStorageSync("IdexSumCount_XZ");
			uni.removeStorageSync("IdexSumCount_JY");
			uni.removeStorageSync("IdexSumCount_YF");
			uni.removeStorageSync("MyAPPUserModel");
			
			var ASSETCODE= uni.getStorageSync("ASSETCODE");
			
			
			let userInfo = uni.getStorageSync("userInfo");
			if (userInfo) {
				if(ASSETCODE==null || ASSETCODE==undefined || ASSETCODE==""){
				uni.navigateTo({
					url: '../home/Index?LoginName=' + JSON.parse(userInfo).userName
				});
				}				
			}else{					
				//读取app.html中设置的客户标识
				var appFlag = uni.getStorageSync("APP_FLAG");
				console.log("appFlag",appFlag);
				if(appFlag=="GTJA"){
					var SSOstr= uni.getStorageSync("SSO");
					if(SSOstr==null || SSOstr==undefined || SSOstr==""){
						this.checkLogin();
					}else{				
						var SSOstrList=SSOstr.split("&");
						if(SSOstrList.length>1){
							var ewsso=SSOstrList[0].substr(1,SSOstrList[0].length-1);
							this.ssologin(ewsso.split("=")[1]);
						}
						
					}
				}
			}
			
			if(ASSETCODE==null || ASSETCODE==undefined || ASSETCODE==""){
				
				}else{
					
					var syshuitiao=uni.getStorageSync("syshuitiao");
					syshuitiao=".."+syshuitiao;
					
					if(syshuitiao=="" || syshuitiao==undefined || syshuitiao==null){
						syshuitiao='../asset/asset?Type=2';
						uni.setStorageSync("syshuitiao",'../asset/asset?Type=2');
					}
					ASSETCODE=ASSETCODE.substr(1,ASSETCODE.length-1);
					var alist=ASSETCODE.split("&");
					var myassetcode="";
					for(var i=0;i<alist.length;i++){
						var str=alist[i];
						var strlist=str.split("=");
						if(strlist.length>1){
							if(strlist[0]=="assetcode"){
								myassetcode=strlist[1];
							}
						}
					}//AssetCode
					if(syshuitiao.indexOf("StockDocumentAsset")>-1){
						//008804-1000329
						uni.navigateTo({
							url: syshuitiao+"&AssetCode="+myassetcode
						});
					}else if(syshuitiao.indexOf("assetedit")>-1){
						uni.navigateTo({
							url: syshuitiao+"&AssetCode="+myassetcode
						});
					}else{
					if(myassetcode!=""){
						uni.navigateTo({
							url: syshuitiao+"&AssetCode="+myassetcode
						});
						/*
						this.request_tool({
								url:'/app/v1/asset/findOne',
								type:'POST',
								data:{
								code:myassetcode
							 }
							}).then(res => {
								console.log(res);
								if(res.data.code==2000){
									uni.navigateTo({
										url: syshuitiao+"&Id="+res.data.data.id
									});
								}else{
										uni.showToast({
											icon: 'none',
											title: "查询不到资产信息"
										});
										uni.navigateTo({
											url: '../home/Index?LoginName=' + JSON.parse(userInfo).userName
										});
									}
					
							}).catch(err => {
						  console.error(err);
						  uni.navigateTo({
						  	url: '../home/Index?LoginName=' + JSON.parse(userInfo).userName
						  });
						});
						*/
						
					}
					}
					return;
				}  
								   
								   
			},
		methods: {			
			checkLogin(){		
				self=this;
				uni.request({										
					url:uni.getStorageSync("localurl")+'/app/v1/auth/gtjaLogin', //仅为示例，并非真实接口地址。
					data: {
					},
					method: "POST",
					success: (res) => {
						console.log(res);
						if(res.data.code==2000){
							
							window.location.href = res.data.data;
						}
					
						},
						fail: () => {
							
							uni.showToast({
								icon: 'none',
								title: '网络异常,请稍后重试'
							});
						}
						});
				/*
				uni.request({
				    url:uni.getStorageSync("localurl")+'/Account/APP_UM_SSOLOGIN',
					data: {
				       
				    },
				    method: "GET",
					success: (res) => {
						uni.hideNavigationBarLoading();						
						 if (res.data.script) {
						        
						        eval(res.data.script);
						    } else {
							      alert("没有返回script");
						    }
					
					},
					fail: () => {
						
						uni.showToast({
							icon: 'none',
							title: '网络异常,请稍后重试'
						});
					}
				    });
					*/
			},
			ssologin(SSOstr){
				console.log(SSOstr);
				//SSOstr="1j3Qwhm8mImuQ0WRdz4iJb9V1z420gpzmXJ73FsJ87abHMoXQIDDHRgBAh4cIK+ywDn9zCOiOMS8UN92Rx9ImSXr1C8wdegstVi/A15cvxfMRCZvT+G32/UwvLj//dd9M/wNU+IRAM/yXO921RqZb9bSKoJV9RiCRq/JiIdeJJynk0sICGSEjKHefSRVh+MYl6s3OT+pXiZAU5FvWV1r5Q==";
				//console.log(SSOstr);
				//uni.deder
				SSOstr=decodeURIComponent(SSOstr);
				uni.request({
				    url:uni.getStorageSync("localurl")+'/app/v1/auth/login',
					data: {
						loginName:SSOstr,
						loginType:  "cas",
					},
				   method: "POST",
					success: (res) => {
						uni.hideNavigationBarLoading();
						console.log("返回");
						console.log(res);
						if (res.data.code=="2000") {
							
							uni.setStorage({
								key: 'LoginName',
								data: {
									LoginName: this.username
								}
							});
							uni.setStorage({
								key: 'userInfo',
								data:JSON.stringify(res.data.map.userInfo)
								
							});
							uni.setStorage({
								key: 'authorization',
								data:res.data.data
								
							});
							uni.setStorage({
								key: 'userToken',
								data:res.data.map.userToken
								
							});
							
							uni.setStorage({
								key: 'userRoles',
								data:res.data.map.userRoles
								
							});
	
							uni.setStorage({
								key: 'userDept',
								data:res.data.map.userDept
								
							});
							uni.setStorage({
								key: 'userReadDeptIds',
								data:res.data.map.userReadDeptIds
								
							});
							uni.setStorage({
								key: 'managePositionIds',
								data:res.data.map.managePositionIds
								
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
							return;
						}
						/*
						tool.data.set("USER_DEPT", loginResult.map.userDept);
								tool.data.set("USER_TOKEN", loginResult.map.userToken);
								tool.data.set("USER_ROLES", loginResult.map.userRoles);
						*/
						uni.setStorageSync('setStatusIndexFunc', 0);
						uni.navigateTo({
							url: '../home/Index?LoginName=' + this.username,
						});
						//window.location.href = 'http://43.254.46.191:7923/MoblieApp/h5/index.html#/pages/home/Index?LoginName=' + this.username
					//window.location.reload();
				
					},
					fail: () => {
						
						uni.showToast({
							icon: 'none',
							title: '网络异常,请稍后重试1'
						});
					}
				
				
				})
			},
			btn() {
				self=this;
				self.isLoading = true;
				if(this.ischeck){
					uni.setStorage({
						key:"syloginname",
						data:this.username
					})
					uni.setStorage({
						key:"syloginpwd",
						data:this.pwd
					})
				}else{
					uni.removeStorageSync("syloginname");
					uni.removeStorageSync("syloginpwd");
				}
				
				if (this.username.length <= 0) {
					uni.showToast({
						icon: 'none',
						title: '请输入用户名'
					});
					return;
				}
				if (this.pwd.length <= 0) {
					uni.showToast({
						icon: 'none',
						title: '请输入密码'
					});
					return;
				}
				
				self.loginStr="登录中...";
				uni.showNavigationBarLoading();//显示加载动画
				console.log("请求"+self.isLoading);
				console.log(uni.getStorageSync("localurl")+'/app/v1/auth/login');
				uni.request({
					url:uni.getStorageSync("localurl")+'/app/v1/auth/login', //仅为示例，并非真实接口地址。
					data: {
						loginName: this.username,
						password: md5(this.pwd),
					},
					method: "POST",
					}).then(data => {
			            let [err, res] = data;
						console.log(res);
						if (res.data.code==2000) {
							
							uni.hideNavigationBarLoading();
							uni.setStorage({
								key: 'LoginName',
								data: {
									LoginName: this.username
								}
							});
							uni.setStorage({
								key: 'userInfo',
								data:JSON.stringify(res.data.map.userInfo)
								
							});
							uni.setStorage({
								key: 'authorization',
								data:res.data.data
								
							});
							uni.setStorage({
								key: 'userToken',
								data:res.data.map.userToken
								
							});
							
							uni.setStorage({
								key: 'userRoles',
								data:res.data.map.userRoles
								
							});
							uni.setStorage({
								key: 'userDept',
								data:res.data.map.userDept
								
							});
							uni.setStorage({
								key: 'userReadDeptIds',
								data:res.data.map.userReadDeptIds
								
							});
							uni.setStorage({
								key: 'managePositionIds',
								data:res.data.map.managePositionIds
								
							});
							self.loginStr="登录";
							uni.setStorageSync('setStatusIndexFunc', 0);
							uni.navigateTo({
								url: '../home/Index?LoginName=' + this.username,
							});
							//managePositionIds
						} else {
							self.loginStr="登录";
							uni.hideNavigationBarLoading();
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
							return;
						}
			
			        }).catch(error => {
			           console.log(error);
			        })
				
				self.isLoading = false;
			},
			checkicon(){
				if(this.ischeck==0){
					this.ischeck=1;
					//uni.setStorage()
					this.checkurl="../../static/images/icon-check.png";
				}else{
					this.ischeck=0;
					this.checkurl="../../static/images/icon-uncheck.png";
				}
			},
			change(){
				
				uni.navigateTo({
					url: '../home/hrefchange'
				});
				//window.location.href = '/moblieapp/h5/index.html#/pages/home/hrefchange';
			},
			request_tool(options){
			 // 在这里可以对请求头进行一些设置
			 let baseUrl = uni.getStorageSync("localurl"); 
			 let userInfo = uni.getStorageSync("userInfo");
			 if (userInfo) {
			 	options.data['userLevelCode'] =JSON.parse(userInfo).levelCode;
			 	options.data['loginUserId'] = JSON.parse(userInfo).id;
			 }
			 let userDept = uni.getStorageSync("userDept");
			 console.log(userDept);
			 if (userDept) {
			 	options.data['loginUserDeptLevelCode'] = userDept.levelCode;
			 	options.data['loginUserDeptId'] = userDept.id;
			 }
			 let currentRole = uni.getStorageSync("userRoles");
			  console.log(currentRole);
			 if (currentRole) {
			 	options.data['roleLevelCode'] = currentRole[0].levelCode;
			 	options.data['roleId'] = currentRole[0].id;
			 }
			 let userReadDeptIds = uni.getStorageSync("userReadDeptIds");
			 
			 if (userReadDeptIds) {
			 	options.data['deptIds'] =userReadDeptIds;
			 }
			  options.header = {
			      'Content-Type':'application/json',
			      Authorization: uni.getStorageSync("authorization")
			 }
			
			    return new Promise((resolve, reject) => {
			        uni.request({
			            url: baseUrl + options.url || '',
			            method: options.type || 'POST',
			            data: options.data || {},
			            header: options.header || {}      
			        }).then(data => {
			            let [err, res] = data;
						if(res.data.code==5001 || res.data.code==5002 ||
						res.data.code==5003 || res.data.code==5004){
							uni.removeStorageSync("LoginName");
							uni.removeStorageSync("userInfo");
							uni.removeStorageSync("authorization");
							uni.removeStorageSync("userToken")
							uni.removeStorageSync("userRoles");
							uni.removeStorageSync("userDept");
							uni.removeStorageSync("userReadDeptIds");
							uni.removeStorageSync("managePositionIds");
							uni.removeStorageSync("ASSETCODE");
							uni.navigateTo({
								url: '../login/login',
							});
						}
			            resolve(res);
			        }).catch(error => {
			            reject(error)
			        })
			    });
			}
		}
	}
</script>

<style>
	

	.gdzcold{
		float:left;
		margin-left: 10rpx;
		color:#6D8AF9;
		font-size: 28px !important;
		margin-top: 150rpx;
		
	}
	.gdzc{
		float:left;
		margin-left: 25rpx;
		color:#FFFFFF;
		font-size: 32px !important;		
		font-weight: bold;
		text-align: left;
		margin-top: 44% !important;
		
	}
	.gdzc_lable{
		float:left;
		margin-left: -47.5%;
		
		color:#FFFFFF;
		font-size: 32rpx !important;				
		font-weight: bold;
		margin-top: 60% !important;
		text-align: left;
		font-family:iconfont;
		
	}
	.item-icon {
		width: 40upx;
		height: 40upx;
	}
	.content {
		min-height: 100%;
		flex: 1;
		flex-direction: column;
		padding-top: 0rpx;
	}
	.content .topcomtent{
		background-color: #6D8AF9;
		height: 600rpx;
		padding-left: 15rpx;
		}

	.btg {
		margin-top: 10px;
	}
	.btg button{
		background-color:#6D8AF9 !important;
		margin-top: 20px;
	}

	.row {
		margin-top: 10rpx;
		display: flex;
		flex-direction: row;
		position: relative;
		background-color: #ffffff;
		margin-bottom: 10px;
		padding-left: 15rpx;
	}

	.group {
		background-color: #ffffff;
		position: relative;
	}

	.row input {
		width: 90%;
		height: 50rpx;
		min-height: 50rpx;
		padding: 15rpx 0;
		padding-right: 30px;
		line-height: 30px;
		margin-left: 30px;
	}

	.row .title {
		width: 30%;
		height: 25px;
		min-height: 25px;
		padding: 15px 0;
		padding-left: 30px;
		line-height: 25px;
		
	}

	.group {
		background-color: #ffffff;
		margin-top: 40px;
		position: relative;
	}
	
	
	/***登录模板 start***/
	.login-screen-title{
		height: 100%;
		text-align: center;
		
	}
	.login-screen-title image {
		width: 216px;
		
	}
	.login-screen-title p{
		color: #004E97;
	    font-size: 24px;
	    text-align: center;
	    margin: 10px 0;
	    font-weight: bold;
	}
	.login-icon-fang{
	    width: 56rpx;
	    height: 56rpx;		
	}
	.login-icon{
	    width: 194px;
	    height: 56px;
		
	}
	
	
	.ItemCheck{
		font-size: 28rpx;
		color: #D3D3D3;
	}
</style>
